<?php

/*
  -----------------------------------------------------------------
  Copyright (c) 2010 25Solutions   All Rights Reserved.

  This software is the confidential and proprietary information
  of 25Solutions ("Confidential Information").
  You shall not disclose such Confidential Information and shall
  use it only in accordance with the terms of the license
  agreement you entered into with 25Solutions
  -----------------------------------------------------------------
 */

/**
 * Description of TfStringUtils
 *
 * @author Fabio Lorenzini <fabio.lorenzini at 25solutions.com>
 */
class TfStringUtils {

    /**
     * Looks for the first occurence of $needle in $haystack and replaces it
     * with $replace.
     * @param string $needle
     * @param string $replace
     * @param string $haystack
     * @return string
     */
    static function strReplaceOnce($needle, $replace, $haystack) {

        $pos = strpos($haystack, $needle);
        if ($pos === false) {
            // Nothing found
            return $haystack;
        }
        return substr_replace($haystack, $replace, $pos, strlen($needle));
    }

     /**
     * Generates the mail with the attributes of the passed Recipient
     * @param string $text
     * @param Recipient $recipient
     * @return string the resulting mail content
     */
    static function replacePlaceHolders($text, Recipient $recipient) {
        $regexp = sfConfig::get('app_template_placeholder_regexp');
        preg_match_all($regexp, $text, $matches, PREG_SET_ORDER);
        foreach ($matches as $match) {
            $placeholder = $match[1];
            $pattern = '/{\$' . $placeholder . '}/';
            try {
                $attributeValue = $recipient->__call('get' . ucfirst($placeholder), array());

                $text = preg_replace($pattern, $attributeValue, $text);
            } catch (Exception $e) {
                //Do nothing
            }
        }
        return $text;
    }

    /**
     * Customize the links in the mail to track user clicks
     * @param string $text
     * @return string the resulting mail content
     */
    static function replaceLinks($text, Recipient $recipient, Template $template, $clickBaseUrl, $campaignId = 0) {
        $regexp = sfConfig::get('app_template_links_regexp');
//        $regexp = '/{\@(\w*)}/';
        preg_match_all($regexp, $text, $matches, PREG_SET_ORDER);
        foreach ($matches as $match) {
            $placeholder = $match[1];
            $pattern = '/{\@' . $placeholder . '}/';
            try {
                //get the link with name $placeholder
                $link = $template->getLink($placeholder);
                $completeTargetUrl = $clickBaseUrl . '?rid=' . $recipient->getRecipientId() . '&lid=' . $link->getLinkId() . '&cid=' . $campaignId;
                $text = preg_replace($pattern, $completeTargetUrl, $text);
            } catch (Exception $e) {
                //Do nothing
            }
        }
        return $text;
    }

    /**
     * Replaces the {#viewonline} tag (if exists in $text) with the url to view the mail
     * in the browser
     * @param string $text the text where to search for the tag
     * @param Recipient $recipient
     * @param integer $campaignId
     * @return string
     */
    static function replaceViewOnlineLink($text, Recipient $recipient, $campaignId = 0) {
        if (strpos($text, sfConfig::get('app_template_show_link'))) {
            $showurl = sfConfig::get('app_show_base_url').'?cid='.$campaignId.'&rid='.$recipient->getRecipientId();
            $text = TfStringUtils::strReplaceOnce(sfConfig::get('app_template_show_link'),
                            $showurl,
                            $text);
        }
        return $text;
    }

}
?>
